/*
 * BundleClReader.java
 *
 * Copyright (c) 2008 Teamnet. All Rights Reserved.
 *
 * This source file may not be copied, modified or redistributed,
 * in whole or in part, in any form or for any reason, without the express
 * written consent of Teamnet.
 */
package ro.prjmanager.bundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ClassPathResource;

import java.util.PropertyResourceBundle;
import java.util.List;
import java.util.ArrayList;
import java.io.InputStream;
import java.io.IOException;

import ro.prjmanager.constants.BackendConstants;

/**
 * This class implements method that parses properties files making possible
 * to read any propertye from it
 */
public class BundleClReader {   
    private Log log = LogFactory.getFactory().getInstance(BundleClReader.class);


    Resource prop;
    String propertiesPath;

    /**
     * The Single constructor of this class
     * @param propertiesPath - The property file path
     */
    private BundleClReader(String propertiesPath) {
        this.propertiesPath = propertiesPath;
        prop = new ClassPathResource(propertiesPath);
    }

    /**
     * Thsi method instantiate an sinle object from this class and after that return it
     * @param propertiesPath - The property file path
     * @return - The single instance of this class
     */
    public static BundleClReader getInstance(String propertiesPath) {
        return new BundleClReader(propertiesPath);

    }

    /**
     * This method find the property with the specified key and returns the value
     * @param key - String key specified
     * @return - The value from properties file asigned to the specified key
     */
    public String getString(String key) {

        String label;
        PropertyResourceBundle ret = null;
        InputStream in;
        try {
            in = prop.getInputStream();

            if (in != null)
                ret = new PropertyResourceBundle(in);
            else
                throw new IllegalStateException(" The bundle name is missing:" + propertiesPath);
        } catch (IOException e) {
            log.error(e);
        }
        label = BackendConstants.MISSING + key;
        if (ret != null) {
            Object temp_ = ret.handleGetObject(key);
            if (temp_ != null) {
                label = (String) temp_;
            }
        }
        return label;
    }

    /**
     * This method finds a list of labels starting from the specified key pattern
     * @param key - String key pattren
     * @param num - int
     * @return list of labels
     */
    public List<String> getCollectionString(String key,int num){
        List<String> strCol=new ArrayList<String>();
        for(int i=1;i<=num;i++){
            String label=getString(key+"."+i);
            if(!label.startsWith(BackendConstants.MISSING))
                strCol.add(label);
        }

        return strCol;
    }
}
